/**
 * \file kernel/device.c
 * \author Davide Gessa
 * \date 01-01-10
 */
#include <driver.h>
#include <device.h>
#include <mm.h>
#include <debug.h>
#include <driver/input/keyboard.h>
#include <driver/input/mouse.h>
#include <driver/crypto/crc32.h>
#include <driver/net/rs232.h>
#include <driver/net/dp8390.h>
#include <driver/crypto/md5.h>
#include <config.h>
#include <driver/bus/pci.h>
#include <driver/video/vesa.h>
#include <fs/devfs.h>
#include <video.h>


/**
 * Aggiorna la lista dei device
 */
void device_update()
{
	device_t *dev;
	device_t *next;
	
	// Svuota la lista
	dev = device_list;
	
	while(dev != NULL)
	{
		next = dev->next;
		free(dev);
		dev = next;
	}
	
	device_list = NULL;
	
	// Rileva i dev
	#ifdef DPCI
		pci_update(&device_list);
	#endif
	
	video_puts_colored(VIDEO_HI_BLUE_BLACK, " -> ");
	printf(MSG_DEV_SCAN "\n");
	
	dev = device_list;
	while(dev != NULL)
	{
		video_puts_colored(VIDEO_HI_BLUE_BLACK, "  -> ");
		printf("0%d:0%d " MSG_DEV_FOUND " '%s - %s'\n", dev->bus, dev->slot, dev->class, dev->class_desc);
		dev = dev->next;
	}
}

/**
 * Restituisce il device numero x
 * 
 * \return stato operazione
 */
uint32_t device_getx(uint32_t x, device_t **dev)
{
	device_t *cf = device_list;
	uint32_t i = 0;
	*dev = NULL;
	
	while(cf != NULL)
	{
		if(i == x)
		{
			*dev = cf;
			return 0;
		}
		i++;
		cf = cf->next;
	}
	return 1;	
}
